fs.ts ➔ readlineSync   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
1
import schema = require("./schema.json")
2
import {
3
  close,
4
  exists,
5
  mkdir,
6
  open,
7
  readFileSync,
8
  rename,
9
  copyFile,
10
  unlink,
11
  writeFile
12
} from "fs"
13
import {tmpdir} from "os"
14
import {join} from "path"
15
import {promisify} from "util"
16
import {randomString} from "./utils"
17
18
const $exists = promisify(exists)
19
, _unlink = promisify(unlink)
20
, $open = promisify(open)
21
, $write = promisify(writeFile)
22
, $close = promisify(close)
23
, $mkdir = promisify(mkdir)
24
, $rename = promisify(rename)
25
, $copy = promisify(copyFile)
26
, tempDir = join(tmpdir(), schema.title)
27
28
export {
29
  readlineSync,
30
  $exists,
31
  $unlink,
32
  $open,
33
  $write,
34
  $close,
35
  $rename,
36
  $copy,
37
  tempFileName,
38
  tempDir
39
}
40
41
//TODO replace with common
42
function readlineSync(path: string, splitter: string) {
43
  return readFileSync(path).toString()
44
  .split(splitter)
45
}
46
47
function $unlink(source: Parameters<typeof _unlink>[0]) {
48
  return $exists(source)
49
  .then(ex => ex ? _unlink(source) : void 0)
50
}
51
52
async function tempFileName() {
53
  await $exists(tempDir) || await $mkdir(tempDir)
54
55
  return join(tempDir, randomString())
56
}
57